<?php
/*
 *      ositems.php
 *
 *      Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

//require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/DB.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/system.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ositem.php");

class ositems {

	//private $object;
	//private $fields;
	//private $owner;
	//public $select;
	private $items = array();
	//private $delete_items = array();
	//private $add_items = array();
	//private $item;
	private $meta;


	function __construct($meta) {
		$this->meta = $meta;
		//$this->object = $object;
		//$this->fields = $fields;
		//$this->owner = $owner;
		//$this->get();
		
	}
	
/*
 * Функция внесения изменений в базу данных
 * name: accept
 * @param
 * @return boolean
 */

	function accept() {
		
	}

	function add($uuid = null) {
		$uuid = $uuid ? $uuid : genuuid();
		//$this->item[$uuid] = new ositem( $uuid, $this->object, $this->owner);
		//$this->item[$uuid] = new ositem($this->object, $uuid);
		//if ($value) {$this->field[$uuid]->value = $value;}
		//return $this->item[$uuid];
		//return $this->add_items[$uuid] = new ositem( $uuid, $this->meta);
		return new ositem( $uuid, $this->meta);
	}
	
	function delete($uuid) {
/*
		if (array_key_exists($uuid,$this->items)) {
			$this->delete_items[$uuid] = $this->items[$uuid];
			unset($this->items[$uuid]);
			return true;
		} else 
		return false;
*/
	}
	
	function markfordeletion($uuid) {
		
	}

	function count() {
		return count($this->items);
	}
	
/*
	private function get() {
		//unset($this->item);
		$sql = "SELECT * FROM ".$this->object->uuid;
		$sql .= $this->owner ? " WHERE owner = '".$this->owner."'" : "";
		$sql .= " ORDER BY ".$this->object->sortby." ".$this->object->direction;
		$sql .= " LIMIT ".$this->object->limit." OFFSET ".$this->object->offset;
		$sql .= ";";
		echo $sql;
		$result = $_SESSION["metadata"]->database->select($sql);
		//echo print_r($result,1);

		foreach ($result as $item) {
			$uuid = $item["id"];
			$this->item[$uuid] = new ositem($this->object, $uuid, $item);
		}

		//return $this->item;
	}
*/
	
/*
	function __get($name) {
		switch ($name) {
			case "item": { break;}
		}
	}
	
	function update() {
		$this->get();
	}
	
*/
	function getall() {
		//$this->get();
		return $this->items;
	}

	function byuuid($uuid) {
		if (array_key_exists($uuid,$this->items)) {
			return $this->items[$uuid];
		} else 
		return null;
	}
}
?>

